﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxEditors;
using System.Configuration;
using System.Collections;

namespace TireService.Website
{
    public partial class Booking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                
            }
        }

        /// <summary>
        /// Gå fra først step til vælg dato
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNext1_Click(object sender, EventArgs e)
        {
            lblStep1.Font.Bold = false;
            lblStep2.Font.Bold = true;

            // Hent ledig kapacitet
            TireServiceDB db = new TireServiceDB();

            int daycapacity = 16;

            rbtnDate.Items.Clear();
            ListEditItem date0 = new ListEditItem();

            date0.Value = 0;
            date0.Text = "Jeg kan ingen af dato'erne";

            rbtnDate.Items.Add(date0);

            List<tmpBooking> booking = db.tmpBookings.ToList();

            //// Dato'er der kan bookes på

            DateTime date3 = Convert.ToDateTime("01-10-2012");
            if (booking.Count(p => p.ServiceDate == date3) <= daycapacity)
            {
                ListEditItem date3itm = new ListEditItem();

                date3itm.Value = "01-10-2012";
                date3itm.Text = "01-10-2012";

                rbtnDate.Items.Add(date3itm);
            }

            DateTime date4 = Convert.ToDateTime("03-10-2012");
            if (booking.Count(p => p.ServiceDate == date4) <= daycapacity)
            {
                ListEditItem date4itm = new ListEditItem();

                date4itm.Value = "03-10-2012";
                date4itm.Text = "03-10-2012";

                rbtnDate.Items.Add(date4itm);
            }

            DateTime date6 = Convert.ToDateTime("05-10-2012");
            if (booking.Count(p => p.ServiceDate == date6) <= daycapacity)
            {
                ListEditItem date6itm = new ListEditItem();

                date6itm.Value = "05-10-2012";
                date6itm.Text = "05-10-2012";

                rbtnDate.Items.Add(date6itm);
            }

            DateTime date5 = Convert.ToDateTime("15-10-2012");
            if (booking.Count(p => p.ServiceDate == date5) <= daycapacity)
            {
                ListEditItem date5itm = new ListEditItem();

                date5itm.Value = "15-10-2012";
                date5itm.Text = "15-10-2012";

                rbtnDate.Items.Add(date5itm);
            }

            DateTime date7 = Convert.ToDateTime("19-10-2012");
            if (booking.Count(p => p.ServiceDate == date7) <= daycapacity)
            {
                ListEditItem date7itm = new ListEditItem();

                date7itm.Value = "19-10-2012";
                date7itm.Text = "19-10-2012";

                rbtnDate.Items.Add(date7itm);
            }

            DateTime date8 = Convert.ToDateTime("29-10-2012");
            if (booking.Count(p => p.ServiceDate == date8) <= daycapacity)
            {
                ListEditItem date8itm = new ListEditItem();

                date8itm.Value = "29-10-2012";
                date8itm.Text = "29-10-2012";

                rbtnDate.Items.Add(date8itm);
            }

            DateTime date9 = Convert.ToDateTime("02-11-2012");
            if (booking.Count(p => p.ServiceDate == date9) <= daycapacity)
            {
                ListEditItem date9itm = new ListEditItem();

                date9itm.Value = "02-11-2012";
                date9itm.Text = "02-11-2012";

                rbtnDate.Items.Add(date9itm);
            }

            DateTime date10 = Convert.ToDateTime("12-11-2012");
            if (booking.Count(p => p.ServiceDate == date10) <= daycapacity)
            {
                ListEditItem date10itm = new ListEditItem();

                date10itm.Value = "12-11-2012";
                date10itm.Text = "12-11-2012";

                rbtnDate.Items.Add(date10itm);
            }

            DateTime date11 = Convert.ToDateTime("16-11-2012");
            if (booking.Count(p => p.ServiceDate == date11) <= daycapacity)
            {
                ListEditItem date11itm = new ListEditItem();

                date11itm.Value = "16-11-2012";
                date11itm.Text = "16-11-2012";

                rbtnDate.Items.Add(date11itm);
            }

            if (rbtnDate.Items.Count == 1)
            {
                // Der er ikke mere kapacitet - giv fejl til brugeren
                pNoMoreCapacity.Visible = true;
                pContactInfo.Visible = false;
            }
            else
            {
                pContactInfo.Visible = false;
                pDate.Visible = true;
            }
        }

        /// <summary>
        /// Gå tilbage til første step - kunde data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrevious1_Click(object sender, EventArgs e)
        {
            lblStep1.Font.Bold = true;
            lblStep2.Font.Bold = false;

            pContactInfo.Visible = true;
            pDate.Visible = false;
        }

        /// <summary>
        /// Gå fra dato valg til valg af tidspunkt eller valg af anden dato/adresse
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNext2_Click(object sender, EventArgs e)
        {
            lblStep2.Font.Bold = false;
            lblStep3.Font.Bold = true;

            pDate.Visible = false;
            pTimeslot.Visible = (rbtnDate.Value.ToString() != "0");
            pDateNotOk.Visible = (rbtnDate.Value.ToString() == "0");

            if (pTimeslot.Visible)
            {
                // Hent ledig kapacitet
                TireServiceDB db = new TireServiceDB();

                rbtnTimeslot.Items.Clear();
                ListEditItem timeslot = new ListEditItem();

                timeslot.Value = 0;
                timeslot.Text = "Jeg kan i alle tidsrum";

                rbtnTimeslot.Items.Add(timeslot);


                List<tmpBooking> booking = db.tmpBookings.ToList();

                int counttimeslot1 = 4 - booking.Count(p => p.Timeslotvalue == 1 & p.ServiceDate == Convert.ToDateTime(rbtnDate.Value));
                int counttimeslot2 = 4 - booking.Count(p => p.Timeslotvalue == 2 & p.ServiceDate == Convert.ToDateTime(rbtnDate.Value));
                int counttimeslot3 = 4 - booking.Count(p => p.Timeslotvalue == 3 & p.ServiceDate == Convert.ToDateTime(rbtnDate.Value));
                int counttimeslot4 = 4 - booking.Count(p => p.Timeslotvalue == 4 & p.ServiceDate == Convert.ToDateTime(rbtnDate.Value));


                if (counttimeslot1 > 0)
                {
                    ListEditItem timeslot1 = new ListEditItem();

                    timeslot1.Value = 1;
                    timeslot1.Text = "8.00-10.00 (" + counttimeslot1.ToString() + " ledige pladser)";

                    rbtnTimeslot.Items.Add(timeslot1);
                }
                if (counttimeslot2 > 0)
                {
                    ListEditItem timeslot2 = new ListEditItem();

                    timeslot2.Value = 2;
                    timeslot2.Text = "10.00-12.00 (" + counttimeslot2.ToString() + " ledige pladser)";

                    rbtnTimeslot.Items.Add(timeslot2);
                }
                if (counttimeslot3 > 0)
                {
                    ListEditItem timeslot3 = new ListEditItem();

                    timeslot3.Value = 3;
                    timeslot3.Text = "12.00-14.00 (" + counttimeslot3.ToString() + " ledige pladser)";

                    rbtnTimeslot.Items.Add(timeslot3);
                }
                if (counttimeslot4 > 0)
                {
                    ListEditItem timeslot4 = new ListEditItem();

                    timeslot4.Value = 4;
                    timeslot4.Text = "14.00-16.00 (" + counttimeslot4.ToString() + " ledige pladser)";

                    rbtnTimeslot.Items.Add(timeslot4);
                }
            }
        }

        /// <summary>
        /// Gå fra valg af tidspunkt til valg af dato
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrevious2_Click(object sender, EventArgs e)
        {
            lblStep2.Font.Bold = true;
            lblStep3.Font.Bold = false;

            pDate.Visible = true;
            pTimeslot.Visible = false;
        }

        /// <summary>
        /// Gå fra valg af tidspunkt til bekræft
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNext3_Click(object sender, EventArgs e)
        {
            lblStep3.Font.Bold = false;
            lblStep4.Font.Bold = true;

            lblSelectedName.Text = "Navn: " + txtName.Text;
            lblSelectedEmail.Text = "Email: " + txtEmail.Text;
            lblSelectedRegNumber.Text = "Registreringsnummer: " + txtRegistrationNumber.Text;
            lblSelectedDate.Text = "Dato: " + rbtnDate.SelectedItem.Text;
            lblSelectedTime.Text = "Tidsrum: " + rbtnTimeslot.SelectedItem.Text;
            lblOther.Text = "";

            pTimeslot.Visible = false;
            pConfirm.Visible = true;
        }

        /// <summary>
        /// Gå fra valg af anden dato/adresse tilbage til dato valg
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrevious2b_Click(object sender, EventArgs e)
        {
            lblStep2.Font.Bold = true;
            lblStep3.Font.Bold = false;

            pDate.Visible = true;
            pDateNotOk.Visible = false;
        }

        /// <summary>
        /// Gå fra valg af anden dato/adresse frem til bekræft
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNext3b_Click(object sender, EventArgs e)
        {
            lblStep3.Font.Bold = false;
            lblStep4.Font.Bold = true;

            string date = "Nej";
            string address = "Nej";

            if (cbNewDate.Checked) date = "Ja";
            if (cbOtherAddress.Checked) address = "Ja";

            lblSelectedName.Text = "Navn: " + txtName.Text;
            lblSelectedEmail.Text = "Email: " + txtEmail.Text;
            lblSelectedRegNumber.Text = "Registreringsnummer: " + txtRegistrationNumber.Text;
            lblSelectedDate.Text = "Dato: " + rbtnDate.SelectedItem.Text;
            //lblSelectedTime.Text = "Tidsrum: " + rbtnTimeslot.SelectedItem.Text;
            lblOther.Text = "Ønsker anden dato: " + date + "<br>Ønsker anden adresse: " + address + "<br>Kommentar: " + txtComment.Text;

            pDateNotOk.Visible = false;
            pConfirm.Visible = true;
        }

        /// <summary>
        /// Gå fra bekræft tilbage til enten valg af dato/adresse eller 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrevious3_Click(object sender, EventArgs e)
        {
            lblStep3.Font.Bold = true;
            lblStep4.Font.Bold = false;

            pConfirm.Visible = false;
            pTimeslot.Visible = (rbtnDate.Value.ToString() != "0");
            pDateNotOk.Visible = (rbtnDate.Value.ToString() == "0");
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            tmpBooking booking = new tmpBooking();

            booking.Name = txtName.Text;
            booking.Email = txtEmail.Text;
            booking.RegNumber = txtRegistrationNumber.Text;
            if (rbtnDate.SelectedItem.Value.ToString() != "0")
            {
                booking.ServiceDate = Convert.ToDateTime(rbtnDate.SelectedItem.Value);
                booking.Timeslotvalue = Convert.ToInt32(rbtnTimeslot.SelectedItem.Value);
                booking.Timeslot = rbtnTimeslot.SelectedItem.Text;
            }

            booking.Note = "Anden dato: " + cbNewDate.Checked + "<br>Anden adresse: " + cbOtherAddress.Checked + "<br>" + txtComment.Text + "<br>Yderligere kommentarer: " + lblOtherComments.Text;

            db.tmpBookings.InsertOnSubmit(booking);

            db.SubmitChanges();

            // Send email
            if (rbtnDate.SelectedItem.Value.ToString() == "0")
            {
                SendEmailReject(booking.Note);
                lblConfirmMessage.Text = "Tak for din tilbagemelding - du vil blive kontaktet hurtigst muligt.";
            }
            else
            {
                SendEmail();
                lblConfirmMessage.Text = "Tak for din godkendelse - en email med din bekræftelse er sendt til dig";
            }
            
            btnConfirm.Visible = false;
            btnPrevious3.Visible = false;
        }

        private void SendEmail()
        {
            string Subject = "Bekræftelse på booking af hjulskift";
            bool test = Convert.ToBoolean(Convert.ToByte(ConfigurationManager.AppSettings["TestEnvironmentOn"]));

            Hashtable templateVars = new Hashtable();

            // Send email to customer
            templateVars.Add("CustomerName", txtName.Text);
            templateVars.Add("RegNumber", txtRegistrationNumber.Text);
            templateVars.Add("ScheduleDate", rbtnDate.SelectedItem.Text);
            templateVars.Add("TimeSchedule", rbtnTimeslot.SelectedItem.Text);
            Shared.Parser parser = new Shared.Parser(Server.MapPath("~/HtmlTemplates/PFA_booking.htm"), templateVars);

            Shared email = new Shared();

            // Send email to customer with us on bcc
            email.SendEmail(txtEmail.Text, ConfigurationManager.AppSettings["EmailAddress"].ToString(), parser.Parse(), Subject, true, test, ConfigurationManager.AppSettings["EmailAddress"].ToString(), null, null);
        }

        private void SendEmailReject(string comment)
        {
            string Subject = "PFA - afvist hjulskift, regnr: " + txtRegistrationNumber.Text;
            bool test = Convert.ToBoolean(Convert.ToByte(ConfigurationManager.AppSettings["TestEnvironmentOn"]));

            Hashtable templateVars = new Hashtable();

            // Send email to customer
            templateVars.Add("CustomerName", txtName.Text);
            templateVars.Add("Email", txtEmail.Text);
            templateVars.Add("RegNumber", txtRegistrationNumber.Text);
            templateVars.Add("RejectInfoComment", comment);
            Shared.Parser parser = new Shared.Parser(Server.MapPath("~/HtmlTemplates/PFA_afvis.htm"), templateVars);

            Shared email = new Shared();

            // Send email us
            email.SendEmail(ConfigurationManager.AppSettings["EmailAddress"].ToString(), ConfigurationManager.AppSettings["EmailAddress"].ToString(), parser.Parse(), Subject, true, test, null, null, null);
        }

    }
}